#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define ll long long
#define N 200010
using namespace std;
int n,a[N];
ll dp[N*5];
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		a[0]=a[1]=0;
		for(int i=2;i<=n+1;i++) scanf("%d ",&a[i]),dp[a[i]]=-LINF;
		n+=2;
		dp[0]=0;
		ll delta=0,mx=0;
		for(int i=2;i<n;i++){
			ll nw=max(mx,dp[a[i]]+delta+a[i]);
			delta+=(a[i]==a[i-1]?a[i]:0);
			mx+=(a[i]==a[i-1]?a[i]:0);
			mx=max(mx,nw);
			dp[a[i-1]]=max(dp[a[i-1]],nw-delta);
		}
		printf("%lld\n",mx);
	}
	return 0;
}